<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .top{
            display: flex;
            justify-content: space-around;
        }
        li{
            text-align: center;
            font-size: 50px;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="top">
            <h2>新闻</h2>
            <h3>换一批</h3>
        </div>
        <ul></ul>
    </div>
</body>
</html>
<script>
    function xtm (fn) {
        var xtm = new XMLHttpRequest()
        xtm.open('get' , './news.json')
        xtm.onreadystatechange = ()=>{
            if(xtm.readyState == 4){
                fn(JSON.parse(xtm.responseText))
            }
        }
        xtm.send()
    }
    
    
    let h3 = document.querySelector('h3')
    let ul = document.querySelector('ul')
    
    function render (){
        xtm(function(data){
            // data.news.forEach((item) => {
            //     var li = document.createElement('li')
            //     li.innerText = item
            //     ul.appendChild(li)
            // });
            for(let i = 0 ; i < data.news.length ; i++){
                let li = document.createElement('li')
                li.innerText = data.news[Math.round(Math.random() * (data.news.length - 1 ))]
                // li.innerText = data.news[2]
                ul.appendChild(li)
            }
        })
    }
    render()
    h3.onclick = ()=> {
        ul.innerHTML = ''
        render()
    }
</script>